package com.ttg.controller;

//~--- non-JDK imports --------------------------------------------------------

import com.ttg.config.Properties;
import com.ttg.contants.KeyContants;
import com.ttg.data.InitGatewayReqData;
import com.ttg.pojo.WithdrawalReqDTO;
import com.ttg.security.SecurityKit;
import com.ttg.utils.HttpKit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

//~--- classes ----------------------------------------------------------------

/**
 * Class GwWithdrawDemoController
 * Description 说明：提现接口测试DEMO
 * Create 2016-08-09 13:37:38
 * @author Ardy
 */
@Controller
@RequestMapping
public class GwWithdrawDemoController {

    /**
     * Field LOGGER
     * Description
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(GwWithdrawDemoController.class);

    /**
     * Method testWithdrawDemo
     * Description 说明：提现接口
     *
     * @return 返回值说明：
     */
    @RequestMapping("/withdraw")
    public ModelAndView testWithdrawDemo() {
        WithdrawalReqDTO withdrawalReq = InitGatewayReqData.initWithdrawal();
        ModelAndView     modelAndView  = new ModelAndView();
        String           url           = "req_withdraw";

        modelAndView.addObject("url", url);
        modelAndView.setViewName("demo/withdraw_req_demo");
        modelAndView.addObject("withdrawal", withdrawalReq);

        return modelAndView;
    }

    /**
     * Method reqWithdraw
     * Description 说明：
     *
     * @param merchantId 说明：
     * @param merOrderId 说明：
     * @param transAmt 说明：
     * @param drCrFlag 说明：
     * @param counterpartMediId 说明：
     * @param thirdtyp 说明：
     * @param usrName 说明：
     * @param openBank 说明：
     * @param prov 说明：
     * @param city 说明：
     * @param purpose 说明：
     * @param subBank 说明：
     * @param feeDirection 说明：
     *
     * @return 返回值说明：
     */
    @RequestMapping(
        value    = "/req_withdraw",
        method   = RequestMethod.POST
    )
    public ModelAndView reqWithdraw(@RequestParam(
        value    = "merchantId",
        required = true
    ) String merchantId, @RequestParam(
        value    = "merOrderId",
        required = true
    ) String merOrderId, @RequestParam(
        value    = "transAmt",
        required = true
    ) Integer transAmt, @RequestParam(
        value    = "drCrFlag",
        required = true
    ) String drCrFlag, @RequestParam(
        value    = "counterpartMediId",
        required = true
    ) String counterpartMediId, @RequestParam(
        value    = "thirdtyp",
        required = true
    ) String thirdtyp, @RequestParam(
        value    = "usrName",
        required = true
    ) String usrName, @RequestParam(
        value    = "openBank",
        required = true
    ) String openBank, @RequestParam(
        value    = "prov",
        required = true
    ) String prov, @RequestParam(
        value    = "city",
        required = true
    ) String city, @RequestParam(
        value    = "purpose",
        required = true
    ) String purpose, @RequestParam(
        value    = "subBank",
        required = true
    ) String subBank, @RequestParam(
        value    = "feeDirection",
        required = false
    ) String feeDirection) {
        WithdrawalReqDTO withdrawalReq = new WithdrawalReqDTO();

        withdrawalReq.setMerchantId(merchantId);
        withdrawalReq.setMerOrderId(merOrderId);
        withdrawalReq.setTransAmt(transAmt);
        withdrawalReq.setDrCrFlag(drCrFlag);
        withdrawalReq.setCounterpartMediId(counterpartMediId);
        withdrawalReq.setThirdtyp(thirdtyp);
        withdrawalReq.setUsrName(usrName);
        withdrawalReq.setOpenBank(openBank);
        withdrawalReq.setProv(prov);
        withdrawalReq.setCity(city);
        withdrawalReq.setPurpose(purpose);
        withdrawalReq.setSubBank(subBank);
        withdrawalReq.setFeeDirection(feeDirection);
        withdrawalReq.setSignatureAlgorithm(KeyContants.SECURITY_TYPE);
        withdrawalReq.setSignatureInfo(
                SecurityKit.signByRSA(HttpKit.httpGwParam2SignStr(withdrawalReq.getAllParameters())));
        ModelAndView modelAndView = new ModelAndView();
        String       url          = Properties.URL.getString("GATEWAY_BASE_SCHEME")
                                    + Properties.URL.getString("WITHDRAW_URL");

        LOGGER.debug("提现请求参数：{},参与签名串：{}", withdrawalReq,
                     HttpKit.httpGwParam2SignStr(withdrawalReq.getAllParameters()));
        modelAndView.addObject("url", url);
        modelAndView.setViewName("req/withdraw_demo");
        modelAndView.addObject("withdrawal", withdrawalReq);

        return modelAndView;
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
